package foodserver;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.subway.babynote.syncdata.ClientDataRecord;
import com.subway.babynote.syncdata.ClientDataRecordBase;
import com.subway.babynote.syncdata.PostParam;
import com.subway.babynote.syncdata.errMessage;

import serverdata.httpreqhandler;


/**
 * Servlet implementation class addbase
 */
//@WebServlet("/addbase")
public abstract class addbase extends getbase {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public addbase() {
        super();
        // TODO Auto-generated constructor stub
    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		 try
		 {		 
			 int iret = 0;
			 JSONObject jsono = this.read_data(request); 
				 //httpreqhandler.readInput(request);		 
			 
			 iret = this.add_data(jsono);			 			 
			 
			 JSONObject return_value = new JSONObject();
			 return_value.put(PostParam.ReturnValue_Param.return_status, iret);			 
			 //return_value.put(PostParam.ReturnValue_Param.err_message, errMessage.err_authenfail.errcode);
			 httpreqhandler.writeOutput(return_value, response);		     			 
		 }
		 catch (JSONException e) {
				// TODO Auto-generated catch block
				System.out.println("JSON error for FeedSync doPost:" + e.getMessage());
				e.printStackTrace();
				
			}
		 catch (Exception e) {
			 System.out.println("FeedSync doPost error:" + e.getMessage());
		      e.printStackTrace();
		    }
	}
    
    protected JSONObject read_data(HttpServletRequest request) 
	{
		JSONObject jsono = null;
		try
		{
			String ur_str = request.getParameter(PostParam.last_avgRecord_Param.records);
			jsono = new JSONObject(ur_str);
			
		}
		catch(Exception ex)
		{
			
		}		
		// TODO Auto-generated method stub
		return jsono;
	}
  
    
    protected abstract int add_data(JSONObject input_jsono);
    
	
    // just a definition
	protected List<ClientDataRecordBase> get_data(ClientDataRecord.user_auth_info uai)
	{
		return null;
	}
	

}
